
#include "ipc.h"
int main(int argc,char *argv[])
{
int rate;
if(argv[1] != NULL) rate = atoi(argv[1]);
else rate = 3; 
buff_key = 101;
buff_num = 1;
pput_key = 102;
pput_num = 1;
shm_flg = IPC_CREAT | 0644;
buff_ptr = (char *)set_shm(buff_key,buff_num,shm_flg);
pput_ptr = (int *)set_shm(pput_key,pput_num,shm_flg);
tobacco_key = 201; 
glue_key = 202;
paper_key=203;  
empty_key = 301;
mutex_key = 302; 
sem_flg = IPC_CREAT | 0644;
sem_val = buff_num;
empty_sem = set_sem(empty_key,sem_val,sem_flg);
sem_val = 0;
paper_sem = set_sem(paper_key,sem_val,sem_flg);
glue_sem = set_sem(glue_key,sem_val,sem_flg);
tobacco_sem = set_sem(tobacco_key,sem_val,sem_flg);
sem_val = 1;
 mutex_sem = set_sem(mutex_key,sem_val,sem_flg);
int i=0;
while(1){
int d;
down(empty_sem);
down(mutex_sem);
buff_ptr[*pput_ptr] = 'A'+ *pput_ptr;
sleep(rate);
d=(i++)%3;
printf("%d provider put: %d to Buffer[%d]\n",getpid(),d,*pput_ptr);
*pput_ptr = (*pput_ptr+1) % buff_num;
up(mutex_sem);
if(d==0)
up(paper_sem);
if(d==1)
up(glue_sem);
if(d==2)
up(tobacco_sem);
}
return EXIT_SUCCESS;
}

